package gui.components;

import gfx.layers.LayerManager;
import gui.listeners.MainWindowButtonListener;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;

public class EditorPanel extends JSplitPane{
  private static final long serialVersionUID = 1L;
  
	private LayerManager layerManager;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel controlsPanel;
	private JPanel layerControlPanel;
	private JPanel layerControlButtonsPanel;
	private Viewport viewport;
	private MainWindowButtonListener mainWindowButtonListener;
	
	public EditorPanel(){
		super(JSplitPane.HORIZONTAL_SPLIT);
		layerManager = new LayerManager();
		
		initialize();		
		initializeViewport();
	}
	
	private void initialize(){
		setResizeWeight(0.75);
		setUI(new BasicSplitPaneUI() {
	    public BasicSplitPaneDivider createDefaultDivider() {
        return new BasicSplitPaneDivider(this) {
          private static final long serialVersionUID = 1L;
					public void setBorder(Border b) {
          }
        };
	    }
		});
		setBorder(null);
		
		add(getLeftPanel(), JSplitPane.LEFT);
		add(getRightPanel(), JSplitPane.RIGHT);		
	}
	
	private void initializeViewport(){
		layerManager.setViewport(viewport);
	}

	public void loadFiles(File[] files){
		for(File f : files){
			layerManager.addLayer(f);
		}
	}
	
	private JPanel getControlsPanel(){
		if(controlsPanel == null){
			controlsPanel = new JPanel(new BorderLayout(5, 5));
			controlsPanel.add(getLayerControlPanel(), BorderLayout.NORTH);
		}
		return controlsPanel;
	}
	
	private JPanel getLayerControlPanel(){
		if(layerControlPanel == null){
			layerControlPanel = new JPanel(new BorderLayout(5, 5));
			layerControlPanel.add(new LayerList(layerManager), BorderLayout.CENTER);
			layerControlPanel.add(getLayerControlButtonsPanel(), BorderLayout.SOUTH);
		}
		return layerControlPanel;
	}
	
	private JPanel getLayerControlButtonsPanel(){
		if(layerControlButtonsPanel == null){
			layerControlButtonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
			JButton createHdrButton = new JButton("Create HDR");
			createHdrButton.addActionListener(getMainWindowButtonListener());
			createHdrButton.setActionCommand(MainWindowButtonListener.CREATE_HDR);
			layerControlButtonsPanel.add(createHdrButton);
		}
		return layerControlButtonsPanel;
	}
	
	private MainWindowButtonListener getMainWindowButtonListener(){
		if(mainWindowButtonListener == null){
			mainWindowButtonListener = new MainWindowButtonListener(layerManager);
		}
		return mainWindowButtonListener;
	}
	
	public LayerManager getLayerManager(){
		return layerManager;
	}
	
	private Viewport getImageViewport(){
		if(viewport == null){
			viewport = new Viewport();
		}
		return viewport;
	}
	
	private JPanel getLeftPanel(){
		if(leftPanel == null){
			leftPanel = new JPanel(new BorderLayout());
			leftPanel.setBorder(new CompoundBorder(
					new EmptyBorder(5, 5, 5, 0), 
					new EtchedBorder(EtchedBorder.LOWERED)));
			leftPanel.add(getImageViewport(), BorderLayout.CENTER);
		}
		return leftPanel;
	}
	
	private JPanel getRightPanel(){
		if(rightPanel == null){
			rightPanel = new JPanel(new BorderLayout());
			rightPanel.setBorder(new EmptyBorder(5, 0, 5, 5));
			rightPanel.add(getControlsPanel(), BorderLayout.CENTER);
		}
		return rightPanel;
	}
}
